package com.app.xinfadi.dao;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.app.xinfadi.base.Constant;
import com.app.xinfadi.base.MyApplication;
import com.app.xinfadi.dao_bean.MartsiteJsonBean;
import com.app.xinfadi.dao_bean.PlantingSiteJsonBean;
import com.app.xinfadi.dao_bean.ProductBeanJsonBean;
import com.app.xinfadi.util.LogUtil;
import com.app.xinfadi.util.SharedPreferencesUtil;
import com.app.xinfadi.widget.gson.GsonRequest;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

/**
 * function: 新建报价计划
 * Created by Wutengfei on 2016/2/1.
 */
public class NewPurchaseDao {
    private static final String TAG = NewPurchaseDao.class.getSimpleName();

    public NewPurchaseDao() {

    }
    /**
     * 获取基地列表
     */
    public void getPlantingsite(Response.Listener<PlantingSiteJsonBean> listener) {
        String URL = Constant.URL + "plantingsiteService/getPlantingsite";
        GsonRequest<PlantingSiteJsonBean> gsonRequest = new GsonRequest<PlantingSiteJsonBean>(Request.Method.POST, URL,
                PlantingSiteJsonBean.class, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                LogUtil.e("LOGIN-ERROR", volleyError.getMessage() + " " + volleyError);
                if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
                    byte[] htmlBodyBytes = volleyError.networkResponse.data;
                    LogUtil.e("LOGIN-ERROR", new String(htmlBodyBytes) + " " + volleyError);
                }

            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
//                params.put("Content-Type","application/json");
                return params;
            }

            @Override
            public byte[] getBody() throws AuthFailureError {
                return super.getBody();
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }

    public void getPlantingsite(TextHttpResponseHandler textHttpResponseHandler) {
        String URL = Constant.URL + "getPlantingsite";
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(18000);
        RequestParams requestParams = new RequestParams();
//        requestParams.put("data", dataJson);
//        requestParams.put("image_data", files);

        client.post(URL, requestParams, textHttpResponseHandler);
    }

    /**
     * 获取采购员
     */
    public void getUsers(Response.Listener<PlantingSiteJsonBean> listener) {
        String URL = Constant.URL + "usersService/getUsers";
        GsonRequest<PlantingSiteJsonBean> gsonRequest = new GsonRequest<PlantingSiteJsonBean>(Request.Method.POST, URL,
                PlantingSiteJsonBean.class, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                LogUtil.e("LOGIN-ERROR", volleyError.getMessage() + " " + volleyError);
                if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
                    byte[] htmlBodyBytes = volleyError.networkResponse.data;
                    LogUtil.e("LOGIN-ERROR", new String(htmlBodyBytes) + " " + volleyError);
                }
            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("user_role", "0");
                return params;
            }

            @Override
            public byte[] getBody() throws AuthFailureError {
                return super.getBody();
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }

    /**
     * 获取市场列表
     */
    public void getMartsite(Response.Listener<MartsiteJsonBean> listener) {
        String URL = Constant.URL + "MartsiteService/getMartsite";
        GsonRequest<MartsiteJsonBean> gsonRequest = new GsonRequest<MartsiteJsonBean>(Request.Method.POST, URL,
                MartsiteJsonBean.class, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                LogUtil.e("LOGIN-ERROR", volleyError.getMessage() + " " + volleyError);
                if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
                    byte[] htmlBodyBytes = volleyError.networkResponse.data;
                    LogUtil.e("LOGIN-ERROR", new String(htmlBodyBytes) + " " + volleyError);
                }
            }
        }) {

            //            @Override
//            protected Map<String, String> getParams() throws AuthFailureError {
//                Map<String, String> params = new HashMap<String, String>();
//                return params;
//            }
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                // Removed this line if you dont need it or Use application/json
//                params.put("Content-Type", "application/x-www-form-urlencoded");
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }
    /*获取采购计划*/
    public void getProduct(final String parent_id, Response.Listener<ProductBeanJsonBean> listener) {
        String URL = Constant.URL + "productsService/getProducts";
        GsonRequest<ProductBeanJsonBean> gsonRequest = new GsonRequest<ProductBeanJsonBean>(Request.Method.POST, URL,
                ProductBeanJsonBean.class, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("parent_id", parent_id);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }
    /*获取所有品类*/
    public void getProduct(Response.Listener<ProductBeanJsonBean> listener) {
        String URL = Constant.URL + "productsService/getProducts";
        GsonRequest<ProductBeanJsonBean> gsonRequest = new GsonRequest<ProductBeanJsonBean>(Request.Method.POST, URL,
                ProductBeanJsonBean.class, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }) {

//            @Override
//            protected Map<String, String> getParams() throws AuthFailureError {
//                Map<String, String> params = new HashMap<String, String>();
//                return params;
//            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }
    /**
     * 新增采购计划
     *
     * @param dataJson
     * @param files
     * @param responseHandler
     * @throws FileNotFoundException
     */
    public void newPurchasePrice(String dataJson, File files[], AsyncHttpResponseHandler responseHandler) throws FileNotFoundException {
        String URL = Constant.URL + "planService/insertPlans";
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(18000);
        RequestParams requestParams = new RequestParams();
        requestParams.put("data", dataJson);
        requestParams.put("image_data", files);
        client.post(URL, requestParams, responseHandler);
    }
}
